/**
 * Anderground project - Subway maps processor application.
 * Copyright (C) 2008-2009 Eldar Abusalimov, Ilya Shurigyn
 * 
 * This file is part of Anderground.
 * 
 * Anderground is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * Anderground is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Anderground.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.anderground.modules.drawer.rapid;

import org.anderground.core.drawer.Calculator;
import org.anderground.core.drawer.Property;
import org.anderground.core.graphics.Canvas;
import org.anderground.core.graphics.Point;
import org.anderground.core.graphics.PointF;
import org.anderground.core.graphics.Rect;
import org.anderground.core.model.Station;
import org.anderground.core.model.Transition;
import org.anderground.modules.drawer.BaseDrawer;

/**
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 */
public class RapidDrawer extends BaseDrawer {

	/** a structure to draw */
	private Structure structure;

	/**
	 * default constructor
	 */
	public RapidDrawer() {
	}

	private Property[] properties;
	/** computed bounds of the model by position values */
	private Rect bounds;

	private void setProperties() {
		Property properties[] = new Property[1];
		String value[] = { "" };
		properties[0] = new Property("Corner Smooth", Property.Type.CHECKBOX,
				value);
		properties[0].setCurrentValue("true");
		this.setProperties(properties);
	}

	@Override
	public Property[] getProperties() {
		return this.properties;
	}

	@Override
	public void setProperties(Property[] newProp) {
		this.properties = newProp;
		// this.internalPrepare();
	}

	@Override
	protected void prepareImpl() {

		if (properties == null)
			setProperties();

		this.structure = new Structure(getModelData(), getMode());

		float minX, minY, maxX, maxY;
		float x, y;
		minX = Float.POSITIVE_INFINITY;
		minY = Float.POSITIVE_INFINITY;
		maxX = Float.NEGATIVE_INFINITY;
		maxY = Float.NEGATIVE_INFINITY;

		PointF coordF = new PointF();
		for (Station station : getModelData().getStations()) {
			coordF.set(station, getMode());
			x = coordF.x;
			y = coordF.y;
			if (x < minX) {
				minX = x;
			}
			if (y < minY) {
				minY = y;
			}
			if (x > maxX) {
				maxX = x;
			}
			if (y > maxY) {
				maxY = y;
			}
		}

		this.bounds = new Rect((int) minX, (int) minY, (int) maxX, (int) maxY);
	}

	@Override
	protected void applyCalculatorImpl(Calculator calculator) {
		for (Node node : structure.getNodesMap().values()) {
			node.applyCalculator(calculator);
		}
	}

	@Override
	protected void drawImpl(Canvas canvas) {
		Node.setup();
		LinkDrawer.setup();

		for (Link link : structure.getLinksMap().values()) {
			link.draw(canvas);
		}

		for (Node node : structure.getNodesMap().values()) {
			node.draw(canvas);
		}
	}

	@Override
	public void selectStationsImpl(Canvas canvas, Station... stations) {
		// TODO Auto-generated method stub

	}

	@Override
	public void selectTransitionsImpl(Canvas canvas, Transition... transitions) {
		// TODO Auto-generated method stub

	}

	@Override
	public Station[] searchStationsImpl(Point point) {
		final int DELTA = 7;
		float minDist = Float.POSITIVE_INFINITY;
		float dist = 0;

		PointF pointF = new PointF();
		Node nearestNode = null;
		for (Node node : structure.getNodesMap().values()) {
			dist = pointF.distanceTo(point);
			if (dist < minDist) {
				minDist = dist;
				nearestNode = node;
			}
		}

		if (nearestNode != null && minDist < DELTA) {
			return (Station[]) nearestNode.getStations().toArray(
					new Station[nearestNode.getStations().size()]);
		}
		return new Station[0];
	}

	@Override
	public Rect getBoundsImpl() {
		return new Rect(this.bounds);
	}

}
